/**
 * 
 */
package dao;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.HashMap;

import model.DON_VI_TCM;
import model.LoaiDonViTCM;
import util.DAL;
import util.MyConnectUnit;

public class DON_VI_TCD {
	private MyConnectUnit conn;
	
	/**
	 * @throws Exception 
	 * 
	 */
	public DON_VI_TCD() throws Exception {
		super();
		this.conn = DAL.getDAL();
	}
	/**
	 * @function get all DON_VI_TC
	 * @param from
	 * @param to
	 * @return
	 * @throws Exception
	 */
	public ArrayList<DON_VI_TCM> getAlls(String ten, int from, int to)
			throws Exception {
		String limit = from + ", " + to;
		ArrayList<DON_VI_TCM> ls = new ArrayList<DON_VI_TCM>();
		ResultSet rs = this.conn.Selects(
						"dv1.id_don_vi_tc, dv1.ma_don_vi, dv1.ten, dv1.cap, dv1.id_don_vi_tc_cha, dv2.ten , dv1.id_loai_don_vi_tc, ldv.ten",
						"DON_VI_TC dv1, DON_VI_TC dv2, LOAI_DON_VI_TC ldv",
						"dv1.id_don_vi_tc_cha = dv2.id_don_vi_tc AND dv1.id_loai_don_vi_tc = ldv.id_loai_don_vi_tc AND dv1.ten LIKE '%"
								+ ten + "%'", "dv1.cap ASC", limit);
		DON_VI_TCM l = null;
		while (rs.next()) {
			l = new DON_VI_TCM();
			l.setId_don_vi_tc(rs.getInt("dv1.id_don_vi_tc"));
			l.setMa_don_vi(rs.getString("dv1.ma_don_vi"));
			l.setTen(rs.getString("dv1.ten"));
			l.setCap(rs.getInt("dv1.cap"));
			
			l.setId_don_vi_tc_cha(rs.getInt("dv1.id_don_vi_tc_cha"));
			DON_VI_TCM dvc = new DON_VI_TCM();
			dvc.setTen(rs.getString("dv2.ten"));
			l.setTen_don_vi_cha(dvc);
			
			l.setId_loai_don_vi_tc(rs.getInt("dv1.id_loai_don_vi_tc"));
			LoaiDonViTCM ldvtc = new LoaiDonViTCM();
			ldvtc.setTen(rs.getString("ldv.ten"));
			l.setTen_loai_don_vi(ldvtc);
			
			ls.add(l);
		}
		return ls;
	}
	
	/**
	 * @function get all DON_VI_TC
	 * @param Condition
	 * @param Limit
	 * @param OrderBy
	 * @return
	 * @throws Exception
	 */
	public ArrayList<DON_VI_TCM> getAll(String Condition, String Limit, String OrderBy) throws Exception {
		ArrayList<DON_VI_TCM> ls = new ArrayList<DON_VI_TCM>();
		ResultSet rs = this.conn.Select("DON_VI_TC", Condition, Limit, OrderBy);
		DON_VI_TCM l = null;
		while (rs.next()) {
			l = new DON_VI_TCM();
			l.setId_don_vi_tc(rs.getInt("id_don_vi_tc"));
			l.setMa_don_vi(rs.getString("ma_don_vi"));
			l.setTen(rs.getString("ten"));
			l.setCap(rs.getInt("cap"));
			l.setId_don_vi_tc_cha(rs.getInt("id_don_vi_tc_cha"));
			l.setId_loai_don_vi_tc(rs.getInt("id_loai_don_vi_tc"));
			ls.add(l);
		}
		return ls;
	}

	/**
	 * @function get all DON_VI_TC with Condition + Limit
	 * @param Condition
	 * @param Limit
	 * @return
	 * @throws Exception
	 */
	public ArrayList<DON_VI_TCM> getAll(String Condition, String Limit) throws Exception {
		return getAll(Condition, Limit, null);
	}

	/**
	 * @function get all DON_VI_TC with Condition
	 * @return
	 * @throws Exception
	 */
	public ArrayList<DON_VI_TCM> getAll(String Condition) throws Exception {
		return getAll(Condition, null);
	}
	
	/**
	 * @function get all DON_VI_TC no parameter
	 * @return
	 * @throws Exception
	 */
	public ArrayList<DON_VI_TCM> getAll() throws Exception {
		return getAll(null);
	}

	/**
	 * @function get id DON_VI_TC
	 * @param id
	 * @return
	 * @throws Exception
	 */
	public DON_VI_TCM getMa(String ma) throws Exception {
		DON_VI_TCM dv = new DON_VI_TCM();
		ArrayList<DON_VI_TCM> list = getAll("ma_don_vi = '" + ma + "'");
		if (list.size() > 0) {
			dv.setId_don_vi_tc(list.get(0).getId_don_vi_tc());
			dv.setMa_don_vi(list.get(0).getMa_don_vi());
			dv.setTen(list.get(0).getTen());
			return dv;
		}
		return null;
	}
	
	/**
	 * @function test Ma DonVi exits
	 * @param ma
	 * @return
	 * @throws Exception
	 */
	public int getMaDonVi(String ma) throws Exception{
		ArrayList<DON_VI_TCM> l = getAll("ma_don_vi = '" + ma + "'");
		return l.size();
	}
	
	
	/**
	 * @function search ten DON_VI_TC
	 * @param ten
	 * @return
	 * @throws Exception
	 */
	public ArrayList<DON_VI_TCM> searchDVTC(String ten) throws Exception {
		ArrayList<DON_VI_TCM> list = getAll("ten LIKE '%" + ten + "%'");
		return list;
	}
	
	/**
	 * @function get all DON_VI_TC with search + paging
	 * @param from
	 * @param to
	 * @return
	 * @throws Exception
	 */
	public ArrayList<DON_VI_TCM> getLimit(String ten, int from, int to) throws Exception {
		String limit = from + ", " + to;
		ArrayList<DON_VI_TCM> list = getAll("ten LIKE '%" + ten + "%'", limit);
		return list;
	}

	/**
	 * function insert DON_VI_TC
	 * @param dv
	 * @return
	 * @throws Exception
	 */
	public boolean Insert(DON_VI_TCM dv) throws Exception {
		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("ma_don_vi", dv.getMa_don_vi());
		map.put("ten", dv.getTen());
		map.put("cap", dv.getCap());
		map.put("id_don_vi_tc_cha",dv.getId_don_vi_tc_cha());
		map.put("id_loai_don_vi_tc",dv.getId_loai_don_vi_tc());
		return this.conn.Insert("DON_VI_TC", map);
	}

	/**
	 * @function update DON_VI_TC
	 * @param dv
	 * @return
	 * @throws Exception
	 */
	public boolean Update(DON_VI_TCM dv) throws Exception {
		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("ma_don_vi", dv.getMa_don_vi());
		map.put("ten", dv.getTen());
		map.put("cap", dv.getCap());
		map.put("id_don_vi_tc_cha",dv.getId_don_vi_tc_cha());
		map.put("id_loai_don_vi_tc",dv.getId_loai_don_vi_tc());
		return this.conn.Update("DON_VI_TC", map, "id_don_vi_tc = " + dv.getId_don_vi_tc());
	}

	/**
	 * @function delete DON_VI_TC
	 * @param id
	 * @return
	 * @throws Exception
	 */
	public boolean Delete(int id) throws Exception {
		return this.conn.Delete("DON_VI_TC", "id_don_vi_tc = " + id);
	}
}
